<?php
namespace app\admin\controller;
use app\admin\logic\IndexLogic;
use app\common\model\User;
use app\common\model\UserInfo;
use app\common\model\UserCredit;
use app\common\model\Order;
use app\common\model\Volunteer;
use app\common\model\Practice;
use app\common\model\SiteTeam;
use app\common\model\Message;
use app\vehicle\logic\TplLogic;
class Index extends Base {

    public function __construct()
    {
        parent::__construct();
    }
    public function index1(){
        $count[] = db('site_team')->where('status','=',0)->count();
        $count[] = db('volunteer')->where('status','=',0)->count();
        $count[] = db('volunteer')->where('o_status','=',0)->count();
        $count[] = db('practice')->where('status','=',0)->count();
        $count[] = db('user')->count();
        $count[] = db('article')->count();
        $count[] = db('practice')->count();
        $count[] = db('forum')->count();
        $this->assign('count',$count);
        $site_list = db('site_team')->limit(6)->order('number desc')->select();
        $num = $site_list[0]['number'];
        foreach($site_list as $k=>$v){
            $site_list[$k]['bai'] = round($v['number']/$num*100);
        }
        $this->assign('site_list',$site_list);
        $wish[] = db('wish')->where('status','=',0)->count();
        $wish[] = db('wish')->where('status','=',1)->where('order_status','=',0)->count();
        $wish[] = db('wish')->where('status','=',1)->where('order_status','=',1)->count();
        $this->assign('wish',$wish);
        return $this->fetch('index1');
    }
    public function get_order(){
       $result =  Order::where('is_read','eq',0)->count();
       exit(json_encode(array('code'=>200,'data'=>$result)));
    }
    public function get_shop_order(){
        $role = session('admin_info');
        $result =  Order::where('is_shop_read','eq',0)->where('bus_id','eq',$role['id'])->count();
        exit(json_encode(array('code'=>200,'data'=>$result)));
    }
    public function tests(){
        $list = db('admin')->where('oid','>',0)->group('oid')->select();
        foreach($list as $k=>$v){
            $count = db('admin')->where('oid','eq',$v['oid'])->count();
            if($count>1){
                echo $v['oid'].',';
            }
        }
    }

    /**
     * 后台首页
     * @return \think\response\View
     */
    public function index(){
        return view();
	}
    public function check_info(){
        $time = time()-86400*6;
        $userx = '';
        $usery = '';
        $volunteery = '';
        for($i=0;$i<7;$i++){
            $t = $time+$i*86400;
            $s_time = strtotime(date('Y-m-d',$t));
            $userx.=','.date('m/d',$t);
            $usery.=','.db('user')->where('create_time','>',$s_time)->where('create_time','>',$s_time)->count();
            $volunteery.=','.db('volunteer')->where('create_time','>',$s_time)->where('create_time','>',$s_time)->count();;
        }
        return json(['userx'=>ltrim($userx,','),'usery'=>ltrim($usery,','),'volunteery'=>ltrim($volunteery,',')]);
    }
	public function home(){
//        $count[] = db('site_team')->where('status','=',0)->count();
//        $count[] = db('volunteer')->where('status','=',0)->count();
//        $count[] = db('volunteer')->where('o_status','=',0)->count();
//        $count[] = db('practice')->where('status','=',0)->count();
//        $count[] = db('user')->count();
//        $count[] = db('article')->count();
//        $count[] = db('practice')->count();
//        $count[] = db('forum')->count();
//        $this->assign('count',$count);
//        $site_list = db('site_team')->limit(6)->order('number desc')->select();
//        $num = $site_list[0]['number'];
//        foreach($site_list as $k=>$v){
//            $site_list[$k]['bai'] = round($v['number']/$num*100);
//        }
//        $this->assign('site_list',$site_list);
//        $wish[] = db('wish')->where('status','=',0)->count();
//        $wish[] = db('wish')->where('status','=',1)->where('order_status','=',0)->count();
//        $wish[] = db('wish')->where('status','=',1)->where('order_status','=',1)->count();
//        $this->assign('wish',$wish);
//        $num = db('volunteer')->count();
//        $arr = [];
//        for($i=0;$i<strlen($num);$i++){
//            $arr[] = substr($num,$i,1);
//        }
//        $this->assign('num', $arr);
//        $time = time()-86400*6;
//        $userx = '';
//        $usery = '';
//        $volunteery = '';
//        for($i=0;$i<7;$i++){
//            $t = $time+$i*86400;
//            $s_time = strtotime(date('Y-m-d',$t));
//            $userx.=','.date('m/d',$t);
//            $usery.=','.db('user')->where('create_time','>',$s_time)->where('create_time','>',$s_time)->count();
//            $volunteery.=','.db('volunteer')->where('create_time','>',$s_time)->where('create_time','>',$s_time)->count();;
//        }
//        $this->assign('userx', ltrim($userx,','));
//        $this->assign('usery', ltrim($usery,','));
//        $this->assign('volunteery', ltrim($volunteery,','));
//        return $this->fetch('index1');
        $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
        $oid = $_SESSION['xm_travel']['admin_info']['oid'];
        if($site_id){
            $count[] = db('volunteer')->where('site_id','eq',$site_id)->count();
        }else if($oid){
            $count[] = db('volunteer')->where('oid','eq',$oid)->count();
        }else {
            $count[] = db('user')->count();
        }
        $count[] = db('article')->count();
        $p = [];
        if($site_id){
            $p[] = ['site_id','=',$site_id];
        }else if($oid){
            $p[] = ['oid','=',$oid];
        }else {
            $p = [];
        }
        $count[] = db('practice')->where($p)->count();
        $count[] = db('forum')->count();
        $this->assign('count',$count);
        $audit[] = SiteTeam::where('status','eq',0)->count();
        $audit[] = Volunteer::where('status','eq',0)->count();
        $audit[] = Volunteer::where('o_status','eq',0)->count();
        $audit[] = Practice::where('status','eq',0)->count();
        $this->assign('audit',$audit);
		return $this->fetch();
	}

	public function getInfo(){
        $business_id = input('business_id');
        $where[] = ['o.status','in','3,7,8'];
        $business_id && $where[] = ['o.business_id','eq',$business_id];
        $time = time();
        for($i=0;$i<15;$i++){
            $d = date('m-d',$time-$i*86400);
            $date[] = $d;
            $dd = date('Y-m-d',$time-$i*86400);
            $start = strtotime($dd.' 00:00:00');
            $end = strtotime($dd.' 23:23:59');
            $price = Order::alias('as o')
                ->join(config('database.prefix').'order_price op','op.order_id=o.id')
                ->where($where)
                ->whereBetween('o.pay_time',[$start,$end])
                ->sum('pay_price');
            $data[] = [$d,$price];
        }
        exit(json_encode(array('data'=>array_reverse($data),'date'=>array_reverse($date))));
    }
}